package com.devops.common.utils;

import cn.hutool.core.util.RandomUtil;

/**
 * @author jxt
 * @Notes
 * @date 2023/10/7 15:06
 */
public class PasswordUtil {
    public static String generatePassword() {
        String password = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER + RandomUtil.BASE_CHAR.toUpperCase() +
                "_", RandomUtil.randomInt(6,18));
        if (isEligible(password)) {
            return password;
        } else {
            return generatePassword();
        }
    }
    private static boolean isEligible(String str){
        boolean hasLetter = str.matches(".*[a-zA-Z].*");
        boolean hasDigit = str.matches(".*[0-9].*");
        boolean hasUnderscore = str.contains("_");

        // 至少有两个条件满足
        return (hasLetter && hasDigit) || (hasLetter && hasUnderscore) || (hasDigit && hasUnderscore);
    }
}
